#!/bin/bash

# 设置颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 项目目录
PROJECT_DIR="$(cd "$(dirname "$0")" && pwd)"
cd "$PROJECT_DIR" || exit 1

echo -e "${BLUE}================================================"
echo -e "    建党在线系统 - 重新构建指定服务"
echo -e "================================================${NC}\n"

# 检查 Docker 是否运行
if ! docker info > /dev/null 2>&1; then
    echo -e "${RED}❌ Docker 未运行，请先启动 Docker Desktop${NC}"
    exit 1
fi

# 检查 docker-compose.yml 是否存在
if [ ! -f "docker-compose.yml" ]; then
    echo -e "${RED}❌ 未找到 docker-compose.yml 文件${NC}"
    exit 1
fi

# 服务列表
services=("backend" "client" "front" "all")
service_names=("后端服务 (backend)" "管理端 (client)" "用户端 (front)" "全部服务")

# 显示服务选择菜单
echo -e "${YELLOW}请选择要重新构建的服务：${NC}"
for i in "${!services[@]}"; do
    index=$((i + 1))
    echo "  $index) ${service_names[$i]}"
done
echo ""

# 读取用户选择
read -p "请选择 (1-4): " choice

# 验证输入
if ! [[ "$choice" =~ ^[1-4]$ ]]; then
    echo -e "${RED}❌ 无效的选择${NC}"
    exit 1
fi

# 获取选择的服务
index=$((choice - 1))
selected_service="${services[$index]}"
selected_name="${service_names[$index]}"

# 是否使用无缓存构建
echo -e "\n${YELLOW}是否使用无缓存构建？(推荐使用，确保完全重新构建)${NC}"
read -p "使用无缓存构建? (Y/n): " no_cache
no_cache=${no_cache:-Y}

build_args=""
if [[ "$no_cache" =~ ^[Yy]$ ]]; then
    build_args="--no-cache"
    echo -e "${BLUE}📦 将使用无缓存模式构建${NC}"
fi

echo -e "\n${BLUE}================================================${NC}"
echo -e "${BLUE}🔨 开始重新构建 ${selected_name}...${NC}"
echo -e "${BLUE}================================================${NC}\n"

# 根据选择执行不同的操作
if [ "$selected_service" = "all" ]; then
    # 重新构建所有服务
    echo -e "${YELLOW}⏸️  停止所有服务...${NC}"
    docker compose down
    
    echo -e "\n${YELLOW}🔨 重新构建所有服务...${NC}"
    if [ -n "$build_args" ]; then
        docker compose build $build_args
    else
        docker compose build
    fi
    
    if [ $? -eq 0 ]; then
        echo -e "\n${YELLOW}🚀 启动所有服务...${NC}"
        docker compose up -d
        
        if [ $? -eq 0 ]; then
            echo -e "\n${GREEN}✅ 所有服务已成功重新构建并启动！${NC}"
        else
            echo -e "\n${RED}❌ 启动服务失败！${NC}"
            exit 1
        fi
    else
        echo -e "\n${RED}❌ 构建服务失败！${NC}"
        exit 1
    fi
else
    # 重新构建指定服务
    echo -e "${YELLOW}⏸️  停止 ${selected_name}...${NC}"
    docker compose stop "$selected_service"
    
    echo -e "\n${YELLOW}🗑️  删除旧容器...${NC}"
    docker compose rm -f "$selected_service"
    
    echo -e "\n${YELLOW}🔨 重新构建 ${selected_name}...${NC}"
    if [ -n "$build_args" ]; then
        docker compose build $build_args "$selected_service"
    else
        docker compose build "$selected_service"
    fi
    
    if [ $? -eq 0 ]; then
        echo -e "\n${YELLOW}🚀 启动 ${selected_name}...${NC}"
        docker compose up -d "$selected_service"
        
        if [ $? -eq 0 ]; then
            echo -e "\n${GREEN}✅ ${selected_name} 已成功重新构建并启动！${NC}"
        else
            echo -e "\n${RED}❌ 启动服务失败！${NC}"
            exit 1
        fi
    else
        echo -e "\n${RED}❌ 构建服务失败！${NC}"
        exit 1
    fi
fi

# 显示服务状态
echo -e "\n${BLUE}📊 当前服务状态：${NC}"
docker compose ps

echo -e "\n${GREEN}================================================${NC}"
echo -e "${GREEN}🎉 操作完成！${NC}"
echo -e "${GREEN}================================================${NC}"

if [ "$selected_service" = "backend" ] || [ "$selected_service" = "all" ]; then
    echo -e "${YELLOW}后端地址:${NC} http://localhost:8080/springboot1i659/"
fi
if [ "$selected_service" = "client" ] || [ "$selected_service" = "all" ]; then
    echo -e "${YELLOW}管理端:${NC} http://localhost:8088"
fi
if [ "$selected_service" = "front" ] || [ "$selected_service" = "all" ]; then
    echo -e "${YELLOW}用户端:${NC} http://localhost:8089"
fi

echo -e "\n${BLUE}💡 查看日志:${NC} docker compose logs -f $selected_service"

